package com.shg.admin.interceptor;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @DESCRIPTION: 自定义一个拦截器，用户拦截每个请求，请求了多少次并放到redis中
 * @USER: shg
 * @DATE: 2023/12/1 1:16
 */
@Component
public class RedisCountUrlInterceptor implements HandlerInterceptor {

    private final StringRedisTemplate stringRedisTemplate;

    public RedisCountUrlInterceptor(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        stringRedisTemplate.opsForValue().increment(requestURI);
        return true;
    }
}
